//
//  GCTranceationAnimation.m
//  GoldChampion
//
//  Created by Mac on 2018/4/13.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "GCTranceationingAnimation.h"
#import "GCFindViewController.h"
#import "GCWokDetailViewController.h"

@implementation GCTranceationingAnimation
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
{
    if (self.pop) {
        return 0.5;
    }
    return 0.8;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    //push
    if (!self.pop) {
        GCFindViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
        
        CGRect originFrame = [fromViewController.imageView convertRect:fromViewController.imageView.bounds toView:fromViewController.view];
        
        
        GCWokDetailViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        UIView *copyVIew = [toViewController.imageView snapshotViewAfterScreenUpdates:YES];
        
        UIView *containerView = [transitionContext containerView];
        containerView.backgroundColor = [UIColor whiteColor];
        
        CGRect finshFrame =  copyVIew.frame;
        copyVIew.frame = originFrame;
        
        toViewController.view.alpha = 0.1;
        
        
        [containerView addSubview:toViewController.view];
        
        UIView * whiteView = [[UIView alloc] initWithFrame:toViewController.imageView.bounds];
        whiteView.backgroundColor = [UIColor whiteColor];
        [containerView addSubview:whiteView];
        [containerView addSubview:copyVIew];
        
        CGFloat during = [self transitionDuration:transitionContext];
        //弹性动画
        [UIView animateWithDuration:during delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0.7 options:UIViewAnimationOptionTransitionNone animations:^{
            copyVIew.frame = finshFrame;
            toViewController.view.alpha = 1;
            
        } completion:^(BOOL finished) {
            [copyVIew removeFromSuperview];
            [whiteView removeFromSuperview];
            toViewController.view.alpha = 1;
            // 必须一定肯定要调用，用来告诉上下文说已经结束动画了
            [transitionContext completeTransition:YES];
        }];
    }

    if (self.pop) {
        GCWokDetailViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
        
        CGRect originFrame = [fromViewController.imageView convertRect:fromViewController.imageView.bounds toView:fromViewController.view];
        UIView * originwhiteView = [[UIView alloc] initWithFrame:originFrame];
        originwhiteView.backgroundColor = [UIColor whiteColor];
        [fromViewController.view addSubview:originwhiteView];
        
        
        GCFindViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        
        
        
        UIView *containerView = [transitionContext containerView];
        containerView.backgroundColor = [UIColor whiteColor];
        
        CGRect imageViewOnViewFrame = [toViewController.imageView convertRect:toViewController.imageView.bounds toView:toViewController.view];
        CGRect finshFrame =  imageViewOnViewFrame;
       
        
        UIImageView * copyVIew = [[UIImageView alloc] initWithFrame:originFrame];
        copyVIew.contentMode = UIViewContentModeScaleAspectFill;
        copyVIew.image = fromViewController.imageView.image;
        copyVIew.clipsToBounds = YES;
//        UIView *copyVIew = [fromViewController.imageView snapshotViewAfterScreenUpdates:YES];
//        copyVIew.frame = originFrame;
        
        
        toViewController.view.alpha = 0.1;
        
        
        [containerView addSubview:toViewController.view];

        UIView * whiteView = [[UIView alloc] initWithFrame:imageViewOnViewFrame];
        whiteView.backgroundColor = [UIColor whiteColor];
        
        [containerView addSubview:whiteView];
        
        [containerView addSubview:copyVIew];
        
        CGFloat during = [self transitionDuration:transitionContext];
        fromViewController.view.alpha = 0;
        //弹性动画
        [UIView animateWithDuration:during delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0.7 options:UIViewAnimationOptionTransitionNone animations:^{
            copyVIew.frame = finshFrame;
            toViewController.view.alpha = 1;
            
        } completion:^(BOOL finished) {
           [copyVIew removeFromSuperview];
            [originwhiteView removeFromSuperview];
            [whiteView removeFromSuperview];
            toViewController.view.alpha = 1;
            // 必须一定肯定要调用，用来告诉上下文说已经结束动画了
            [transitionContext completeTransition:YES];
        }];
    }
}
@end

